package net.coding.bamboo.wex.fundmanage;

import net.coding.bamboo.wex.fundmanage.security.RSACoder;
import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class DecodeServerSocket {
	public static final Logger mylog = LoggerFactory.getLogger(DecodeServerSocket.class);

	public static final String ksrcbPrivateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAIhyyKBobxP1MZRS+D0/rxArACA8H/U8bmPnLF8Wl1wM1xnHRjp+j/3hSLlSHBw34kzZmSIy1YKtpAbFxbuxpPoLLwTQIAQQg7wEZtaEpsEHe6B4fVg/yoWKogslb3FT2KNUN06peskn22VnbqmXnalNdCWbMcV0sfDOH/zZ7i29AgMBAAECgYAtuAAfNz7YuhRVtYM1yovW6AGPy3WNzyTR1piYd9JZZngATDPsz5FhqMWcSrXegp8RzusveUbvFU1bc5Lwc/Jp0xj2uRIKPTFkVML/u7FMbCtvxdS1cj4oFzx6y/1uhqm/ROTZeJXF7l4FqtKn1t5uTv/MK2pdTWStscfR7RkLxQJBANgjc86YjfxFLyy1H/SOINbUlCqgtJuMcw2qwULwaqmB2w5PN/Bf/rZ3XLI7q9mXk5sY9OkOI2lArby56ORYcKcCQQChnOvtURiRG1YtDQZynM3HhYInFNnvEZe8TDeoXcVp+LPLQxlmPHESBDc8r4zW1zeX6hfWnHL14whCW1Iq97b7AkEAvnr4C6G8uA/M1GzHvcIoRMxRzAOXR+KOXwQWkETK65oqkMDV/rzLAMnO4rDd8cS47B4hcObp5E+BDesVyW/7zwJAH2+yxLrdmHxEywaZlnnB3Wmu44M+0S4O4eXBfdKfr/lcn9Exg52GMQb456kOMEw02CwE7w4HnqTG2oGA/jKpOQJASl8but2HVg1fZLVrE419bZ9b4L9h+kBVCn/Gr6XbDG++4CC0OR8qY3mr7G5Zapx63s+aG25Tv1PT0IMrHTMkcA==";

	public static void main(String[] args) {
		try {
			ServerSocket server = new ServerSocket(99);
			Socket socket;
			while (true) {
				socket = server.accept();
				String length = readNumByte(socket, 8);
				System.out.println("长度：" + length);

				byte[] encodeResponseBytes = readByte(socket, Integer.valueOf(length));
				String encodeReq = new String(encodeResponseBytes, "GB18030");
				System.out.println("密文：" + encodeReq);

//				Init.init();
//				byte[] base64req = com.sun.org.apache.xml.internal.security.utils.Base64.decode(encodeResponseBytes);
//				byte[] decodedData = RSACoder.decryptByPrivateKey(base64req, ksrcbPrivateKey);
//				String reqStr = new String(decodedData, "GB18030");
//				System.out.println("客户端请求Base64：" + reqStr);

				byte[] base64req = Base64.decodeBase64(encodeResponseBytes);
				byte[] decodedData = RSACoder.decryptByPrivateKey(base64req, ksrcbPrivateKey);
				System.out.println("Base64测试：" + new String(decodedData, "GB18030"));

//				byte[] decodedData = RSACoder.decryptByPrivateKey(encodeResponseBytes, ksrcbPrivateKey);
//				String responseStr = new String(decodedData, "GB18030");
//				System.out.println("客户端内容：" + responseStr);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}


	/**
	 * 读取流
	 *
	 * @param input
	 * @return
	 */
	public static String getInput(InputStream input) {
		int count = 0;

		if (input != null) {
			try {
				while (count == 0) {
					count = input.available();
				}
				byte[] buffer = new byte[count];
				input.read(buffer);
//				input.close();
				return new String(buffer).toString();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		return null;
	}

	public static String readNumByte(Socket socket, int size) throws Exception {
		byte[] num = new byte[size];
		int readBytes = 0;
		while (readBytes < size) {
			int readNum = socket.getInputStream().read(num, readBytes, size - readBytes);
			if (readNum == -1) {
				break;
			}
			readBytes += readNum;
		}

		return new String(num, "GB18030");

	}


	public static byte[] readByte(Socket socket, int size) throws Exception {
		byte[] num = new byte[size];
		int readBytes = 0;
		while (readBytes < size) {
			int readNum = socket.getInputStream().read(num, readBytes, size - readBytes);
			if (readNum == -1) {
				break;
			}
			readBytes += readNum;
		}

		return num;
	}
}
